﻿/*****************************************************************
// Copyright (C)
// All rights reserved.
// 
// Author:       WYQ
// Create Date:  2012-8-18 15:16:01
// Usage: 持久化数据库仓库
*****************************************************************/
using System;
using System.Linq;
using System.Linq.Expressions;

namespace Portal.Ef.Repository
{


    public class MenuRepository : Repository<Menu, Menu, TbMenu>, IMenuRepository<Menu>
    {
        public override Menu CreateEntity()
        {
            return new Menu { };
        }
        public override IQueryable<Menu> Get()
        {
            return MapQueryable(Entities.TbMenu);
        }
        public override long Count()
        {
            return MapQueryable(Entities.TbMenu).Count();
        }
        public override IQueryable<Menu> Get(Expression<Func<Menu, bool>> expression)
        {
            return MapQueryable(Entities.TbMenu).Where(expression);
        }
        public override IQueryable<Menu> GetSort(string sort = "Id", string order = "DESC")
        {
            Check.Argument.IsNotNull(sort, "排序字段");
            Check.Argument.IsNotNull(order, "排序条件");
            return LinqHelper.DataSorting(MapQueryable(Entities.TbMenu), sort, order);
        }
        public override IQueryable<Menu> GetSort(Expression<Func<Menu, bool>> expression, string sort = "Id", string order = "DESC")
        {
            Check.Argument.IsNotNull(expression, "查询条件");
            Check.Argument.IsNotNull(sort, "排序字段");
            Check.Argument.IsNotNull(order, "排序条件");
            return LinqHelper.DataSorting(MapQueryable(Entities.TbMenu).Where(expression), sort, order);
        }
        public override void Add(Menu t)
        {
            Entities.AddToTbMenu(ReverseMapModel(t));
        }
        public override void Update(Menu t)
        {
            throw new NotImplementedException();
        }
        public override void AddOrUpdate(Menu t)
        {
            if (Entities.TbMenu.Any(o => o.CoMenuId == t.Id))
            {

            }
            else
            {
                Entities.AddToTbMenu(ReverseMapModel(t));
            }
        }
        public override void Delete(Menu t)
        {
            Entities.TbMenu.Where(o => o.CoMenuId == t.Id).ForEach(o => Entities.DeleteObject(o));
        }
        public override void Delete(Expression<Func<Menu, bool>> expression)
        {
            MapQueryable(Entities.TbMenu).Where(expression).ForEach(o => Entities.DeleteObject(o));
        }
        public override void Save()
        {
            Entities.SaveChanges();
        }

        public JqEasyuiDataGrid<Menu> JqEasyuiDataGrid(string code, string name, string categoryId, string sort = "CreateTime", string order = "Desc", int page = 10, int index = 1)
        {
            return new JqEasyuiDataGrid<Menu>();
        }

        public override sealed IQueryable<Menu> MapQueryable(IQueryable<TbMenu> queryable)
        {
            return queryable.Select(o => new Menu
            {

                Id = o.CoMenuId,
                Name = o.CoName
,
                Url = o.CoUrl
,
                Icon = o.CoIcon
,
                ParentId = o.CoParentId
,
                Depth = o.CoDepth ?? 0
,
                CreateTime = o.CoCreateTime ?? DateTime.Now
,
                IsDelete = o.CoIsDelete ?? false
,
                Version = o.CoVersion ?? 0
,
                IsClosed = o.CoIsClosed ?? false
,
                Culture = o.CoCulture
,
                Sort = o.CoSort ?? 0
,
                ActionName = o.CoActionName
,
                ControllerName = o.CoControllerName
,
                Area = o.CoArea

            });
        }

        public override sealed IQueryable<TbMenu> ReverseMapQueryable(IQueryable<Menu> queryable)
        {
            return queryable.Select(o => new TbMenu
            {

                CoMenuId = o.Id,
                CoName = o.Name
,
                CoUrl = o.Url
,
                CoIcon = o.Icon
,
                CoParentId = o.ParentId
,
                CoDepth = o.Depth
,
                CoCreateTime = o.CreateTime
,
                CoIsDelete = o.IsDelete
,
                CoVersion = o.Version
,
                CoIsClosed = o.IsClosed
,
                CoCulture = o.Culture
,
                CoSort = o.Sort
,
                CoActionName = o.ActionName
,
                CoControllerName = o.ControllerName
,
                CoArea = o.Area


            });
        }

        public override sealed Menu MapModel(TbMenu model)
        {
            return new Menu
            {
                Id = model.CoMenuId,
                Name = model.CoName
                ,
                Url = model.CoUrl
                ,
                Icon = model.CoIcon
                ,
                ParentId = model.CoParentId
                ,
                Depth = model.CoDepth ?? 0
                ,
                CreateTime = model.CoCreateTime ?? DateTime.Now
                ,
                IsDelete = model.CoIsDelete ?? false
                ,
                Version = model.CoVersion ?? 0
                ,
                IsClosed = model.CoIsClosed ?? false
                ,
                Culture = model.CoCulture
                ,
                Sort = model.CoSort ?? 0
                ,
                ActionName = model.CoActionName
                ,
                ControllerName = model.CoControllerName
                ,
                Area = model.CoArea
            };
        }

        public override sealed TbMenu ReverseMapModel(Menu model)
        {
            return new TbMenu
            {
                CoMenuId = model.Id,
                CoName = model.Name

                ,
                CoUrl = model.Url

                ,
                CoIcon = model.Icon

                ,
                CoParentId = model.ParentId

                ,
                CoDepth = model.Depth

                ,
                CoCreateTime = model.CreateTime

                ,
                CoIsDelete = model.IsDelete

                ,
                CoVersion = model.Version

                ,
                CoIsClosed = model.IsClosed

                ,
                CoCulture = model.Culture

                ,
                CoSort = model.Sort

                ,
                CoActionName = model.ActionName

                ,
                CoControllerName = model.ControllerName

                ,
                CoArea = model.Area



            };
        }


    }
}



